//mine.js
//获取应用实例
const app = getApp()

Page({
  data: {
    logo:"/imgs/logo.png"
  },
  getUserInfo: function (e) {
    var that = this;
    wx.login({
      success: function (response) {
        console.log(response)
        console.log("1")
        var code = response.code
        wx.getUserInfo({
          success: function (resp) {
            console.log(resp)
            console.log("2")
            console.log(code)
            console.log(resp.iv)
            console.log(resp.encryptedData)
            var uI = resp.userInfo;
            wx.request({
              url: 'https://soya.91soya.com/onLogin',
              data: {
                code: code,
                iv: resp.iv,
                encryptedData: resp.encryptedData
              },
              success: function (res) {
                console.log(res)
                console.log("3")
                app.globalData.userInfo = e.detail.userInfo;
                app.globalData.openId = res.data.openId;
                wx.request({
                  url: 'https://soya.91soya.com/api/wxcreate',
                  method: "post",
                  data: {
                    openid: res.data.openId,
                    avatarUrl: uI.avatarUrl,
                    country: uI.country,
                    city: uI.city,
                    gender: uI.gender,
                    language: uI.language,
                    nickName: uI.nickName,
                    province: uI.province
                  },
                  success: function () {
                    if (that.data.back == "back") {
                      wx.navigateBack({
                        delta: 1,
                      })
                    } else {
                      wx.switchTab({
                        url: '/pages/index/index',
                      })
                    }
                  }
                })
              },
              fail: function () {
                console.log("登录失败")
              }
            })
          },
        })
      },
      fail: function () {}
    });
   
  },
  onLoad: function (e) {
    this.setData({
      back: e.back
    })
    if (app.globalData.userInfo) {
      var that = this;
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
      wx.request({
        url: 'https://soya.91soya.com/api/ordernum/' + app.globalData.openId,
        method: "get",
        success: function (res) {
          that.setData({
            orderNum: res.data.ordernum,
            storeNum: res.data.collnum
          })
        }
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }

    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })

    }
  },


})